# coding:utf8
from flask import jsonify

from appserver import db_store
from sqlalchemy.orm import relationship, backref

from models.entity.model_base import ModelBase
from models.entity.tree_model_base import TreeModelBase

"""
项目应用表
"""


class Project_Item(ModelBase):
    __tablename__ = "project_items"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'项目编号')
    name = db_store.Column(db_store.String(64), comment=u"项目名称")
    project_id = db_store.Column(db_store.Integer, db_store.ForeignKey("projects.id"), nullable=False)
    type = db_store.Column(db_store.String(255), nullable=False, comment=u"类型")


    def __repr__(self):
        return "<Project %s>" % self.name

    def get_project_item_by_id(self, id):
        return self.query.filter_by(id=id).first()

    def get_project_item_by_name(self, name):
        return self.query.filter_by(name=name).first()

    def get_project_item_by_type(self, type):
        return self.query.filter_by(type=type).first()

    def get_project_item_by_project_id(self, project_id):
        project_items = self.query.filter_by(project_id=project_id).all()
        tmp = {}
        data = []
        print(1111)
        for item in project_items:
            data.append(item.to_dict())
            print(2222)
        # tmp['success'] = 1
        # tmp['error_code'] = None
        # tmp['error_msg'] = None
        # tmp['data'] = data
        return data

